package com.lanlian.cayman.widget;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.lanlian.cayman.R;
import com.lanlian.cayman.activity.KLineActivityBase;
import com.lanlian.cayman.dialog.ColorPopup;
import com.lanlian.cayman.dialog.StylePopup;
import com.lanlian.cayman.utils.TimeUtil;
import com.lanlian.utillib.ImageUtils;

import java.io.File;
import java.util.Date;

public class StyleUtil extends LinearLayout implements View.OnClickListener{
    private int color;

    private LinearLayout mLlHengline;
    private LinearLayout mLlXieline;
    private LinearLayout mLlShuline;
    private LinearLayout mLlMoreline;
    private LinearLayout mLlRectline;
    private LinearLayout mLlClose;

    private LinearLayout mLl1;
    private LinearLayout mLl2;
    private LinearLayout mLlRevoke;
    private LinearLayout mLlColor;
    private LinearLayout mLlLine;
    private LinearLayout mLlEdit;
    private LinearLayout mLlJieping;
    private LinearLayout mLlDelete;

   private MyImageView myimage;


    KLineActivityBase act;

    public StyleUtil(Context context) {
        super(context);
       initView(context);

    }

    public StyleUtil(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);

    }
    private void initView(Context context)
    {
       act=(KLineActivityBase) context;
        LayoutInflater inflater=act.getLayoutInflater();
        LinearLayout ll=new LinearLayout(act);
        View contentView = inflater.inflate(R.layout.view_drawutil, ll,false);
        myimage=contentView.findViewById(R.id.myimage);
        mLlHengline = contentView.findViewById(R.id.ll_hengline);
        mLlXieline = contentView.findViewById(R.id.ll_xieline);
        mLlShuline = contentView.findViewById(R.id.ll_shuline);
        mLlMoreline = contentView.findViewById(R.id.ll_moreline);
        mLlRectline = contentView.findViewById(R.id.ll_rectline);
        mLlClose = contentView.findViewById(R.id.ll_close);


        mLl1 = contentView.findViewById(R.id.ll1);
        mLl2 = contentView.findViewById(R.id.ll2);
        mLlRevoke = contentView.findViewById(R.id.ll_revoke);
        mLlColor = contentView.findViewById(R.id.ll_color);
        mLlLine = contentView.findViewById(R.id.ll_line);
        mLlEdit = contentView.findViewById(R.id.ll_edit);
        mLlJieping = contentView.findViewById(R.id.ll_jieping);
        mLlDelete = contentView.findViewById(R.id.ll_delete);

        mLlRevoke.setOnClickListener(this);
        mLlColor.setOnClickListener(this);
        mLlLine.setOnClickListener(this);
        mLlEdit .setOnClickListener(this);
        mLlJieping.setOnClickListener(this);
        mLlDelete.setOnClickListener(this);


        mLlHengline.setOnClickListener(this);
        mLlXieline.setOnClickListener(this);
        mLlShuline.setOnClickListener(this);
        mLlShuline.setOnClickListener(this);
        mLlMoreline.setOnClickListener(this);
        mLlRectline.setOnClickListener(this);
        mLlClose.setOnClickListener(this);


        addView(contentView);
    }
    @Override
    public void onClick(View v) {
        Intent intent=new Intent();
        int id = v.getId();
        switch (id)
        {
            case R.id.ll_close:
                setVisibility(GONE);
                break;
            case R.id.ll_edit:
                setSelect(1);
                break;
            case R.id.ll_xieline:
                act.kline.kLineView.styleView.setStyle(act.kline.kLineView.styleView.STYLE_TWO_POINT);
                setSelect(2);
                break;
            case R.id.ll_hengline:
                act.kline.kLineView.styleView.setStyle(act.kline.kLineView.styleView.STYLE_HENGLINE);
                setSelect(2);
                break;
            case R.id.ll_shuline:
                act.kline.kLineView.styleView.setStyle(act.kline.kLineView.styleView.STYLE_SHULINE);
                setSelect(2);
                break;
            case R.id.ll_moreline:
                act.kline.kLineView.styleView.setStyle(act.kline.kLineView.styleView.STYLE_THREE_POINT);
                setSelect(2);
                break;
            case R.id.ll_rectline:
                act.kline.kLineView.styleView.setStyle(act.kline.kLineView.styleView.STYLE_RECT);
                setSelect(2);
                break;


            case R.id.ll_color:
                if(act.kline.kLineView.styleView.getState()==0)
                {
                    act.showToast("请选择线的样式");
                    return;
                }
                ColorPopup cd=new ColorPopup(act,this);
                cd.showPopup(act.kline.kLineView);
                break;
            case R.id.ll_line:
                if(act.kline.kLineView.styleView.getState()==0)
                {
                    act.showToast("请选择线的样式");
                    return;
                }
                StylePopup sp=new StylePopup(act);
                sp.showPopup(act.kline.kLineView);
                break;
            case R.id.ll_delete:
                act.kline.kLineView.styleView.delete();
                break;
            case R.id.ll_revoke:
                act.kline.kLineView.styleView.invoke();
                break;
            case R.id.ll_jieping:
               jieping();
                break;
            default:
                break;

        }
    }

    /**
     *
     * @param index 1选择画点 2编辑样式
     */
    public void setSelect(int index)
    {
        if(index==1) {
            mLl1.setVisibility(VISIBLE);
            mLl2.setVisibility(GONE);
        }
        if(index==2) {
            mLl1.setVisibility(GONE);
            mLl2.setVisibility(VISIBLE);
        }
        myimage.setColor(act.kline.kLineView.styleView.getColor());
    }

    public void jieping()
    {

        act.getWindow().getDecorView().setDrawingCacheEnabled(true);
        act.getWindow().getDecorView().buildDrawingCache();
        Bitmap bitmap=act.getWindow().getDecorView().getDrawingCache();
       String s= ImageUtils.saveBitmap(bitmap,TimeUtil.getDate(null)+"jieping.png");
       if(!"".equals(s))
       {
           act.showToast("截屏成功");
           File file=new File(s);
           //通知相册更新
           MediaStore.Images.Media.insertImage(act.getContentResolver(), BitmapFactory.decodeFile(file.getAbsolutePath()),file.getName(), null);
           Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
           Uri uri = Uri.fromFile(file);
           intent.setData(uri);
           act.sendBroadcast(intent);

       }
       else
       {
           act.showToast("截屏失败");
       }
    }
    public void show()
    {
        mLl1.setVisibility(VISIBLE);
        mLl2.setVisibility(GONE);
        setVisibility(VISIBLE);
    }
    public int getColor() {
        return color;
    }

    public void setColor(int color) {
        this.color = color;
        myimage.setColor(act.kline.kLineView.styleView.getColor());
    }
}
